var c = 0;
var t;
var timer_is_on = 0;

function timedCount() {
	document.getElementById('counter').value = "Eltelt idő: " + c + "s";
	c = c + 1;
	t = setTimeout("timedCount()", 1000);
}

function doTimer() {
	if (!timer_is_on) {
		timer_is_on = 1;
		timedCount();

		document.getElementById("continue").className = "button disabled";
		document.getElementById("continue").disable = true;

		document.getElementById("pause").className = "button";
		document.getElementById("pause").disable = false;

	}
}

function stopCount() {
	clearTimeout(t);
	timer_is_on = 0;

	document.getElementById("continue").className = "button";
	document.getElementById("continue").disable = false;

	document.getElementById("pause").className = "button disabled";
	document.getElementById("pause").disable = true;
	return false;
}

function newGame() {
	window.location = "/Mancala/index.jsp";
}
function continueGame() {
	window.location = "/Mancala/game.jsp";
}

function numbersonly(e, locator) {
	document.getElementById(locator).maxLength = 2;
	var unicode = e.charCode ? e.charCode : e.keyCode;
	if (unicode != 8) { // if the key isn't the backspace key (which we should
		// allow)
		if (unicode < 48 || unicode > 57) // if not a number
			return false; // disable key press
	}
	return true;
}

function validateForm() {
	var x = document.getElementById("name").value;
	if (x == null || x == "") {
		alert("Adj meg egy nevet!");
		return false;
	}
	return true;
}

function enableButton(button) {
	button.className = "button";
	button.disabled = false;
}

function enableHoleButtons() {
	var inputs = document.getElementsByTagName("input");
	for ( var i = 0; i < inputs.length; i++) {
		if (inputs[i].id.indexOf("user") == 0) {
			enableButton(inputs[i]);
		}
	}
}

function disableButton(button) {
	button.className = "button disabled";
	button.disabled = true;
}

function disableAllButtons() {
	var inputs = document.getElementsByTagName("input");
	for ( var i = 0; i < inputs.length; i++) {
		if (inputs[i].id == "start" || inputs[i].id == "continue" || inputs[i].id == "pause"
				|| inputs[i].id == "next" || inputs[i].id == "counter") {
			continue;
		}
		disableButton(inputs[i]);
	}
}

function getXmlHttpRequest() {
	if (typeof XMLHttpRequest == "undefined")
		XMLHttpRequest = function() {
			try {
				return new ActiveXObject("Msxml2.XMLHTTP.6.0");
			} catch (e) {
			}
			try {
				return new ActiveXObject("Msxml2.XMLHTTP.3.0");
			} catch (e) {
			}
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			}
			// Microsoft.XMLHTTP points to Msxml2.XMLHTTP.3.0 and is redundant
			throw new Error("This browser does not support XMLHttpRequest.");
		};
	return new XMLHttpRequest();
}

function init() {
	var xhr = getXmlHttpRequest();
	c = 0;
	var params = "param=init";
	xhr.open("post", "stepping", true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {
			console.log("ajax response");
			doTimer();
			document.getElementById("start").value = "Restart";
			enableHoleButtons();

			var result = JSON.parse(xhr.responseText);
			var index = result.state.length;
			console.log("index: " + index);
			console.log("table state: " + result.state);

			// computer point color
			document.getElementsByTagName("tr")[0]
					.getElementsByTagName("input")[0].style.backgroundColor = "red";

			// computer table
			for ( var i = 0; i < index / 2; i++) {
				document.getElementsByTagName("tr")[0]
						.getElementsByTagName("input")[i].value = result.state[i];

			}

			// user table
			var helper = 0;
			for ( var i = index / 2; i < index; i++) {
				document.getElementsByTagName("tr")[1]
						.getElementsByTagName("input")[helper].value = result.state[i];

				// user points
				if (i == index - 1) {
					disableButton(document.getElementsByTagName("tr")[1]
							.getElementsByTagName("input")[helper]);
					// user points color
					document.getElementsByTagName("tr")[1]
							.getElementsByTagName("input")[helper].style.backgroundColor = "red";
				}

				helper++;
			}
		}

	};
	console.log("ajax request");
	xhr.send(params);
}

function move(col) {
	var xhr = getXmlHttpRequest();

	var params = "param=" + col;
	xhr.open("post", "stepping", true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {

			console.log("ajax response");

			var result = JSON.parse(xhr.responseText);

			console.log("tableList[0]" + result.tableList[0].state);
			console.log("tableList[1]" + result.tableList[1].state);

			changingTheTable(result.tableList[0]);

			// user next step
			if (result.tableList[0].stateAfterStep == 1) {
				console.log("stateAfterStep == 1");
				checkUserButtons(result.tableList[1].state.length, true);
			} else {
				checkUserButtons(result.tableList[1].state.length, false);
				setTimeout(changingTheTable, 2000, result.tableList[1]);
			}

			// user next step
			if (result.tableList[0].stateAfterStep == 3
					|| result.tableList[1].stateAfterStep == 3) {
				ending();
			}
		}

	};
	console.log("ajax request");
	xhr.send(params);
}

function changingTheTable(table) {

	var index = table.state.length;
	console.log("index: " + index);
	console.log("table state " + table.state);

	// computer table
	for ( var i = 0; i < index / 2; i++) {
		document.getElementsByTagName("tr")[0].getElementsByTagName("input")[i].value = table.state[i];

	}

	// user table
	var helper = 0;
	for ( var i = index / 2; i < index; i++) {
		document.getElementsByTagName("tr")[1].getElementsByTagName("input")[helper].value = table.state[i];

		if (table.state[i] == 0) {
			disableButton(document.getElementsByTagName("tr")[1]
					.getElementsByTagName("input")[helper]);
		} else {
			enableButton(document.getElementsByTagName("tr")[1]
					.getElementsByTagName("input")[helper]);
		}
		// user points
		if (i == index - 1) {
			disableButton(document.getElementsByTagName("tr")[1]
					.getElementsByTagName("input")[helper]);
		}

		helper++;
	}
}
function checkUserButtons(index, user) {
	for ( var i = 0; i < index / 2 - 1; i++) {
		if (user) {
			if (document.getElementsByTagName("tr")[0]
					.getElementsByTagName("input")[i].value != 0) {
				enableButton(document.getElementsByTagName("tr")[1]
						.getElementsByTagName("input")[i]);
			}
		} else {
			disableButton(document.getElementsByTagName("tr")[1]
					.getElementsByTagName("input")[i]);
		}
	}
}
function ending() {
	// var xhr = getXmlHttpRequest();
	// var params = "param=end&time="+c;
	// xhr.open("post", "score", true);
	// xhr.setRequestHeader("Content-type",
	// "application/x-www-form-urlencoded");
	// xhr.onreadystatechange = function() {
	window.location = "/Mancala/score?param=end&time=" + c;
	// };
	// console.log("ajax request");
	// xhr.send(params);
}
